package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.*;
import java.util.concurrent.locks.Condition;

@Service
public class ItemCatServiceImp implements ItemCatService {
    @Autowired
    private ItemCatMapper itemCatMapper;

    public Map<Integer, List<ItemCat>> getMap() {
        Map<Integer, List<ItemCat>> map = new HashMap<>();
        //查询所有数据
        List<ItemCat> itemCatList = itemCatMapper.selectList(null);
        for (ItemCat itemCat : itemCatList) {
            Integer key = itemCat.getParentId();
            if (map.containsKey(key)) {
                map.get(key).add(itemCat);
            } else {
                List<ItemCat> list = new ArrayList<>();
                list.add(itemCat);
                map.put(key, list);
            }
        }
        return map;
    }

    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        Map<Integer, List<ItemCat>> map = getMap();
        if (level == 1) {
            return map.get(0);
        }
        if (level == 2) {
            return getTwoList(map);
        }

        return getThreeList(map);

    }

    @Transactional
    @Override
    public void saveItem(ItemCat itemCat) {
//        Date date = new Date();
        itemCat.setStatus(true);
        itemCatMapper.insert(itemCat);
    }

    @Transactional
    @Override
    public void deleteItemCat(Integer id, Integer level) {
        if (level == 3) {
            itemCatMapper.deleteById(id);
        }
        if (level == 2) {
            itemCatMapper.delete(new QueryWrapper<ItemCat>().eq("parent_id", id).or().eq("id", id));
        }
        if (level == 1) {
            List twoIds = itemCatMapper.selectObjs(new QueryWrapper<ItemCat>().eq("parent_id", id));
            itemCatMapper.delete(new QueryWrapper<ItemCat>().in(twoIds.size() > 0, "parent_id", twoIds)
                    .or().eq(twoIds.size() > 0, "parent_id", id)
                    .eq("id", id));
        }
    }
@Transactional
    @Override
    public void updateStatus(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }
@Transactional
    @Override
    public void updateItemCat(ItemCat itemCat) {
        ItemCat temp = new ItemCat();
        temp.setId(itemCat.getId())
                .setName(itemCat.getName())
                ;
        itemCatMapper.updateById(temp);
    }

    private List<ItemCat> getTwoList(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = map.get(0);
        for (ItemCat oneItemCat : oneList) {
            int parentId = oneItemCat.getId();
            List<ItemCat> twoList = map.get(parentId);
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }

//    @Override
//    public List<ItemCat> findItemCatList(Integer level) {
//        QueryWrapper queryWrapper =new QueryWrapper();
//        queryWrapper.eq("parent_id", 0);
//       List<ItemCat> oneList =  itemMapper.selectList(queryWrapper);
//        for (ItemCat oneItemCat:
//             oneList) {
//            queryWrapper.clear();
//            int oneId = oneItemCat.getId();
//            queryWrapper.eq("parent_id", oneId);
//            List<ItemCat> twoList = itemMapper.selectList(queryWrapper);
//            oneItemCat.setChildren(twoList);
//            for (ItemCat twoItemCat:twoList){
//                int twoId = twoItemCat.getId();
//                List<ItemCat> threeList = itemMapper.selectList(new QueryWrapper<ItemCat>().eq("parent_id", twoId));
//                twoItemCat.setChildren(threeList);
//            }
//        }
//        return oneList;
//    }


    private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = getTwoList(map);
        for (ItemCat oneItemCat : oneList) {

            List<ItemCat> twoList = oneItemCat.getChildren();
            if (twoList == null || twoList.size() == 0) {
                continue;
            }
            for (ItemCat twoItemCat : twoList) {
                Integer twoId = twoItemCat.getId();
                List<ItemCat> threeList = map.get(twoId);
                twoItemCat.setChildren(threeList);
            }

        }
        return oneList;
    }

}
